package com.likersai.opon.system.exception;

import com.likersai.opon.pojo.bo.ResponseBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

import static com.likersai.opon.constant.ExceptionMsg.WORST;

/**
 * 全局异常处理
 *
 * @author sun.ye
 * @since 2021-02-26 21:44
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseBO<Exception> handlerCommonException (Exception e) {
        e.printStackTrace();
        return ResponseBO.getException(WORST,e);
    }

    @ExceptionHandler(OponException.class)
    public <T> ResponseBO<T> handleOponException(OponException e){
        return ResponseBO.getException(e.getMsgCode());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseBO<List<String>> handleParamValidException(MethodArgumentNotValidException e){
        BindingResult bindingResult = e.getBindingResult();
        return ResponseBO.getFailureValid(bindingResult);
    }

}